﻿Imports System.Xml
Imports System.Reflection
Imports Mysoft.Map.Workflow.Types
Imports Mysoft.Map.Workflow.BusinessDomain
Imports Mysoft.Map.Data
Imports System.Text

Public Class YearPGSP
    Inherits BusinessDomainBase

    Private _businessGUID As String     '业务GUID
    Private _xml As XmlDocument = New XmlDocument

    Public Sub New()
        MyBase.New("/PubProject/workflow/cgztb/YearPGSP.xml")
        _xml.LoadXml(MyBase.XML)
    End Sub

    '属性：业务GUID
    Public Overrides Property BusinessGUID() As String
        Get
            Return _businessGUID
        End Get
        Set(ByVal Value As String)
            _businessGUID = Value
        End Set
    End Property

    '属性：XML
    '最终XML格式：

    Public Overrides Property XML() As String
        Get
            Return _xml.OuterXml
        End Get
        Set(ByVal Value As String)
            _xml.LoadXml(Value)
        End Set
    End Property

    '功能：取业务数据，填充XML
    Public Overrides Function LoadData() As Integer
        Dim strSQL As String
        Dim dtTemp As DataTable
        Dim iRowCount, i As Integer

        '主记录    
        strSQL = "SELECT YearPGGUID,PGSubject,PGDate, PGZrr, PGScope, PGInfo,ApproveState,BUGUID" & _
                 " FROM p_YearPG " & _
                 " WHERE YearPGGUID='" & Me._businessGUID & "'"

        dtTemp = MyDB.GetDataTable(strSQL)
        If dtTemp.Rows.Count > 0 Then

            '根节点业务GUID属性
            _xml.SelectSingleNode("/BusinessType").Attributes.GetNamedItem("BusinessGUID").Value = Me._businessGUID

            '获取主记录,用于显示到扩展表单上
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='公司GUID']").InnerText = dtTemp.Rows(0).Item("BUGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='评估主题']").InnerText = dtTemp.Rows(0).Item("PGSubject").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='评估日期']").InnerText = Format(DateTime.Parse(dtTemp.Rows(0).Item("PGDate").ToString), "yyyy-MM-dd")
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='责任人']").InnerText = dtTemp.Rows(0).Item("PGZrr").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='评估范围']").InnerText = dtTemp.Rows(0).Item("PGScope").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='评估说明']").InnerText = dtTemp.Rows(0).Item("PGInfo").ToString
            '_xml.SelectSingleNode("/BusinessType/Item/Domain[@name='产品与服务名称编码']").InnerText = Format(Decimal.Parse(dtTemp.Rows(0).Item("Score").ToString), "##0.00")

            '供应商列表    
            strSQL = "SELECT YearPGGUID,ProviderGUID,ProviderName,ZHLevel," & _
                     "CASE WHEN HZLevel IS NULL THEN '未合作' WHEN HZLevel ='' THEN '未合作' WHEN HZLevel IS not NULL THEN HZLevel END AS HZLevel," & _
                     "CASE WHEN ZZPGScore IS NULL THEN '-' WHEN ZZPGScore IS not NULL THEN '<a href=""javascript:viewProviderZZPG('''+cast(ProviderGUID AS varchar(50))+''')"">'+convert(varchar(100),ZZPGScore,1)+'</a>' END AS ZZPGScore," & _
                     "CASE WHEN LYPGScore IS NULL THEN '-' WHEN LYPGScore IS not NULL THEN '<a href=""javascript:viewProviderLYPG('''+cast(ProviderGUID AS varchar(50))+''')"">'+convert(varchar(100),LYPGScore,1)+'</a>' END AS LYPGScore," & _
                     "JYZHLevel " & _
                     "FROM vp_YearPG " & _
                     "WHERE YearPGGUID='" & Me._businessGUID & "' ORDER BY ProviderName ASC"

            dtTemp = MyDB.GetDataTable(strSQL)
            iRowCount = dtTemp.Rows.Count - 1
            '由于供应商可以一条都没有,所以应该先删再复制行
            Dim group As XmlNode = _xml.SelectSingleNode("/BusinessType/Item/Group")
            Dim item As XmlNode = group.SelectSingleNode("Item")
            If Not group Is Nothing Then
                group.RemoveAll()
            End If

            If iRowCount >= 0 Then
                For i = 0 To iRowCount
                    Dim newXmlNode As XmlElement = item.Clone()
                    newXmlNode.SelectSingleNode("Domain[@name='供应商GUID']").InnerText = dtTemp.Rows(i).Item("ProviderGUID").ToString
                    newXmlNode.SelectSingleNode("Domain[@name='供应商名称']").InnerText = dtTemp.Rows(i).Item("ProviderName").ToString
                    newXmlNode.SelectSingleNode("Domain[@name='综合等级']").InnerText = dtTemp.Rows(i).Item("ZHLevel").ToString
                    newXmlNode.SelectSingleNode("Domain[@name='合作等级']").InnerText = dtTemp.Rows(i).Item("HZLevel").ToString
                    newXmlNode.SelectSingleNode("Domain[@name='资质评估得分']").InnerText = dtTemp.Rows(i).Item("ZZPGScore").ToString
                    newXmlNode.SelectSingleNode("Domain[@name='履约评估得分']").InnerText = dtTemp.Rows(i).Item("LYPGScore").ToString
                    newXmlNode.SelectSingleNode("Domain[@name='建议综合等级']").InnerText = dtTemp.Rows(i).Item("JYZHLevel").ToString
                    group.AppendChild(newXmlNode)
                Next
            Else
                '当没有数据时,保留一条空行,用于同步时匹配不出错,但是这条空行在问卷列表是不会加载的,这是在列表前台控制的 add by wangtao on 2010.03.18
                Dim newXmlNode As XmlElement = item.Clone()
                newXmlNode.SelectSingleNode("Domain[@name='供应商GUID']").InnerText = ""
                newXmlNode.SelectSingleNode("Domain[@name='供应商名称']").InnerText = ""
                newXmlNode.SelectSingleNode("Domain[@name='综合等级']").InnerText = ""
                newXmlNode.SelectSingleNode("Domain[@name='合作等级']").InnerText = ""
                newXmlNode.SelectSingleNode("Domain[@name='资质评估得分']").InnerText = ""
                newXmlNode.SelectSingleNode("Domain[@name='履约评估得分']").InnerText = ""
                newXmlNode.SelectSingleNode("Domain[@name='建议综合等级']").InnerText = ""
                group.AppendChild(newXmlNode)
            End If
        End If

        Return 0
    End Function

    '功能：根据XML进行数据归档
    Public Overrides Function SaveData() As String
        Dim strYearPGGUID, strJYZHLevel, strProductsAndServiceCode, strProviderGUID, strBUGUID, strNow, strSQL As String
        Dim dtTmp As DataTable
        Dim sbALL As New StringBuilder

        Dim strProductTypeCodeList As New StringBuilder
        Dim intArrLen, intListLength, intIndex, i As Integer
        Dim arrProductsAndServiceCode As String()

        '获取进行部分资质评估信息
        '资质评估GUID
        strYearPGGUID = Me._businessGUID
        sbALL.Append("UPDATE p_YearPG SET ApproveState='已审核' WHERE YearPGGUID='" & strYearPGGUID & "';")
        Dim group As XmlNode = _xml.SelectSingleNode("/BusinessType/Item/Group")
        Dim item As XmlNodeList = group.SelectNodes("Item")
        If item.Count > 0 Then
            For i = 0 To item.Count - 1
                sbALL.Append("UPDATE p_Provider SET ZHLevel='" & item(i).SelectSingleNode("Domain[@name='建议综合等级']").InnerText.Replace(",", "") & "' WHERE ProviderGUID='" & item(i).SelectSingleNode("Domain[@name='供应商GUID']").InnerText.Replace(",", "") & "';")
            Next
        End If

        Try
            i = MyDB.ExecSQL(sbALL.ToString)
            If i > 0 Then
                Return "<xml result=""true"" errormessage=""""></xml>"
            Else
                Return "<xml result=""false"" errormessage=""审批失败，请稍后再试！""></xml>"
            End If
        Catch ex As Exception
            Return "<xml result=""false"" errormessage=""审批失败，请稍后再试！""></xml>"
        End Try
    End Function

    '功能：发起审批
    Public Overrides Function Initiate() As String
        Dim strSQL As String
        Dim intRtn As Integer
        '发起审批时,同时更新资质评估的审核状态为审核中
        strSQL = "UPDATE p_YearPG SET ApproveState='审核中' WHERE YearPGGUID='" & Me._businessGUID & "';"
        intRtn = MyDB.ExecSQL(strSQL)
        If intRtn = -1 Then
            Return "<xml result=""false"" errormessage=""审批失败，请稍后再试！""></xml>"
        End If

        '返回执行是否成功的信息
        Return "<xml result=""true"" errormessage=""""></xml>"

    End Function

    '功能：审批不通过
    Public Overrides Function SaveData_NoPass() As String
        Dim strSQL As String
        '审批不通过,更新资质评估的审核状态为未审核
        strSQL = "UPDATE p_YearPG SET ApproveState='未审核' WHERE YearPGGUID = '" & Me._businessGUID & "'"
        MyDB.ExecSQL(strSQL)

        Return "<xml result=""true"" errormessage=""""></xml>"
    End Function

    '功能：发起撤回
    Public Overrides Function InitiateWithDrawn() As String

        '由于发起撤回的审批流程和审批不通过处理是一样的，所以直接调用审批不通过处理的方法即可
        Return SaveData_NoPass()

    End Function

    '功能：审批作废处理
    Public Overrides Function SaveData_Cancel() As String

        '由于审批作废处理流程和审批不通过处理是一样的，所以直接调用审批不通过处理的方法即可
        Return SaveData_NoPass()
    End Function

    '功能：审批通过后再驳回数据处理
    Public Function SaveData_RollBack() As String

        '由于审批驳回处理流程和审批不通过处理是一样的，所以直接调用审批不通过处理的方法即可
        Return SaveData_NoPass()
    End Function
End Class
